package com.fdq.demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

@Component
public class SpringMailComponent {

    @Value("${spring.mail.username}")
    private String from;
    @Autowired
    private JavaMailSender mailSender;

    public void send(String target, String title, String content){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(ConstantPropertiesForEmail.TARGET);
        if (!ObjectUtils.isEmpty(target)){
            message.setTo(target);
        }
        message.setSubject(ConstantPropertiesForEmail.TITLE);
        message.setText(content);
        mailSender.send(message);
    }
}
